﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace TestControl
{
    public class SelectableUserControl:UserControl
    {
        protected static Pen selectedPen;
        public Panel basePnl;
        /*
        protected bool selected;

        public bool getSelected()
        {
            return selected;
        }

        public void setSelected(bool value)
        {
            selected = value;
            if (!selected)
            {
                selectedPen = new Pen(Color.Green);
                selectedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            }
            else
            {
                selectedPen = new Pen(Color.Red);
                selectedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            }
            basePnl.Enabled = selected;
            basePnl.Invalidate();
            
        }
        */

        public void select()
        {
            selectedPen = new Pen(Color.Green);
            selectedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            /*
            foreach (Control c in basePnl.Controls)
            {
                c.Enabled = true;
            }
             * */
            basePnl.Refresh();
        }

        public void unSelect()
        {
            selectedPen = new Pen(Color.Black);
            selectedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            /*
            foreach (Control c in basePnl.Controls)
            {
                c.Enabled = false;
            }
             * */
            basePnl.Refresh();
        }

        static SelectableUserControl()
        {
            selectedPen = new Pen(Color.Black);
            selectedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
        }

        public SelectableUserControl()
        {
            InitializeComponent();
            this.Click += new EventHandler(basePnl_Click);
            basePnl.Paint += new PaintEventHandler(basePnl_Paint);
        }


        private void basePnl_Click(object sender, EventArgs e)
        {
            select();
            basePnl.Enabled=true;
        }
        
        protected void basePnl_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(selectedPen,
                                     e.ClipRectangle.Left,
                                     e.ClipRectangle.Top,
                                     e.ClipRectangle.Width - 1,
                                     e.ClipRectangle.Height - 1);
            e.Graphics.DrawRectangle(selectedPen,
                                     e.ClipRectangle.Left+2,
                                     e.ClipRectangle.Top+2,
                                     e.ClipRectangle.Width - 5,
                                     e.ClipRectangle.Height - 5);
        }
        
        protected void drawBorder(Pen p)
        {
            this.CreateGraphics().DrawRectangle(selectedPen,
                                    basePnl.Location.X,
                                    basePnl.Location.Y,
                                    basePnl.Size.Width - 1,
                                    basePnl.Size.Height - 1);
            this.CreateGraphics().DrawRectangle(selectedPen,
                                    basePnl.Location.X + 2,
                                    basePnl.Location.Y + 2,
                                    basePnl.Size.Width - 5,
                                    basePnl.Size.Height - 5);
        }

        private void InitializeComponent()
        {
            this.basePnl = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // basePnl
            // 
            this.basePnl.Dock = System.Windows.Forms.DockStyle.Fill;
            this.basePnl.Location = new System.Drawing.Point(0, 0);
            this.basePnl.Name = "basePnl";
            this.basePnl.Size = new System.Drawing.Size(150, 150);
            this.basePnl.TabIndex = 0;
       //     this.basePnl.Paint += new PaintEventHandler(basePnl_Paint);
            // 
            // SelectableUserControl
            // 
            this.Controls.Add(this.basePnl);
            this.Name = "SelectableUserControl";
            this.ResumeLayout(false);

        }


    }
}
